package com.flxz.user.controller;

import com.flxz.common.annotation.Log;
import com.flxz.common.core.controller.BaseController;
import com.flxz.common.core.domain.AjaxResult;
import com.flxz.common.core.page.TableDataInfo;
import com.flxz.common.enums.BusinessType;
import com.flxz.user.domain.UserExpense;
import com.flxz.user.service.IUserExpenseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户支出Controller
 * 
 * @author flxz
 * @date 2024-01-15
 */
@RestController
@RequestMapping("/user/expense")
public class UserExpenseController extends BaseController
{
    @Autowired
    private IUserExpenseService userExpenseService;

    /**
     * 查询用户支出列表
     */
    @PreAuthorize("@ss.hasRole('common') or @ss.hasPermi('user:expense:list')")
    @GetMapping("/list")
    public TableDataInfo list(UserExpense userExpense)
    {
        startPage();
        Long userId = getUserId();
        userExpense.setUserId(userId);
        List<UserExpense> list = userExpenseService.selectUserExpenseList(userExpense);
        return getDataTable(list);
    }

    /**
     * 获取用户支出详细信息
     */
    @PreAuthorize("@ss.hasRole('common') or @ss.hasPermi('user:expense:query')")
    @GetMapping(value = "/{expenseId}")
    public AjaxResult getInfo(@PathVariable("expenseId") Long expenseId)
    {
        return success(userExpenseService.selectUserExpenseByExpenseId(expenseId));
    }

    /**
     * 新增用户支出
     */
    @PreAuthorize("@ss.hasRole('common') or @ss.hasPermi('user:expense:add')")
    @Log(title = "用户支出", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UserExpense userExpense)
    {
        Long userId = getUserId();
        userExpense.setUserId(userId);
        return toAjax(userExpenseService.insertUserExpense(userExpense));
    }

    /**
     * 修改用户支出
     */
    @PreAuthorize("@ss.hasRole('common') or @ss.hasPermi('user:expense:edit')")
    @Log(title = "用户支出", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UserExpense userExpense)
    {
        Long userId = getUserId();
        userExpense.setUserId(userId);
        return toAjax(userExpenseService.updateUserExpense(userExpense));
    }

    /**
     * 修改用户支出（通过ID）
     */
    @PreAuthorize("@ss.hasRole('common') or @ss.hasPermi('user:expense:edit')")
    @Log(title = "用户支出", businessType = BusinessType.UPDATE)
    @PutMapping("/{expenseId}")
    public AjaxResult editById(@PathVariable Long expenseId, @RequestBody UserExpense userExpense)
    {
        Long userId = getUserId();
        userExpense.setUserId(userId);
        userExpense.setExpenseId(expenseId);
        return toAjax(userExpenseService.updateUserExpense(userExpense));
    }

    /**
     * 删除用户支出
     */
    @PreAuthorize("@ss.hasRole('common') or @ss.hasPermi('user:expense:remove')")
    @Log(title = "用户支出", businessType = BusinessType.DELETE)
    @DeleteMapping("/{expenseIds}")
    public AjaxResult remove(@PathVariable Long[] expenseIds)
    {
        return toAjax(userExpenseService.deleteUserExpenseByExpenseIds(expenseIds));
    }

    /**
     * 删除单个用户支出
     */
    @PreAuthorize("@ss.hasRole('common') or @ss.hasPermi('user:expense:remove')")
    @Log(title = "用户支出", businessType = BusinessType.DELETE)
    @DeleteMapping("/{expenseId}")
    public AjaxResult removeSingle(@PathVariable Long expenseId)
    {
        return toAjax(userExpenseService.deleteUserExpenseByExpenseId(expenseId));
    }
}
